package com.css.dzj.dzjportal.common.util.loginInfo;

import com.css.dzj.dzjportal.common.util.Restful.RestfulUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@Component
@PropertySource(value = "classpath:base.properties")
public class GetLoginInfo {
    @Value("${BASE_URL}")
    private String BASE_URL;

    /**
     * 根据用户ID获取用户信息
     *
     * @param request
     * @return
     */
    public HashMap getUserInfo(HttpServletRequest request) {
        HttpSession session = request.getSession();
        // 根据用户ID获取用户信息
        String userId = (String) session.getAttribute("userId");
        String url = BASE_URL + "/platform/api/org/users/" + userId;
        Map<String, String> map = new HashMap<>();
        HttpMethod method = HttpMethod.GET;
        Map body = RestfulUtil.sendRestful(request, map, url, method);
        Map data = (Map) body.get("data");

        // 存储用户信息
        HashMap<String, Object> user = new HashMap();
        user.put("loginName", data.get("userCode").toString());
        user.put("userId", data.get("userId").toString());
        // 用户角色list
        user.put("role", getRole(request));
        // 用户当前组织信息
        Map orgs = getOrgsByUserId(request);
        if (orgs != null) {
            user.put("orgName", (String) orgs.get("deptName"));
            user.put("orgLevel", (Integer) orgs.get("deptLevel"));
            // 用户父组织信息
            Map parentOrgs = null;
            if (orgs.get("parentId") != null) {
                parentOrgs = getOrgsByDeptId(request, (String) orgs.get("parentId"));
                user.put("parentOrgName", (String) parentOrgs.get("deptName"));
                user.put("parentOrgLevel", (String) parentOrgs.get("deptLevel"));
            }
        }
        Map sudapps = getSudappsByUserId(request);
        // 用户拥有的系统list
        List<Map<String, Object>> sysApp = (List<Map<String, Object>>) sudapps.get("data");
        user.put("sysApp", sysApp);
        // token
        user.put("accessToken", session.getAttribute("token").toString());
        return user;
    }

    /**
     * 获取用户角色
     */
    public List<String> getRole(HttpServletRequest request) {
        HttpSession session = request.getSession();
        String userId = (String) session.getAttribute("userId");
        String url = BASE_URL + "/platform/api/org/users/" + userId + "/roles";
        HttpMethod method = HttpMethod.GET;
        Map map = new HashMap();
        Map body = RestfulUtil.sendRestful(request, map, url, method);
        ArrayList list = (ArrayList) body.get("data");
        return list;
    }

    /**
     * 根据(userId)用户ID获取用户所在机构信息
     */
    public Map getOrgsByUserId(HttpServletRequest request) {
        HttpSession session = request.getSession();
        String userId = (String) session.getAttribute("userId");
        String url = BASE_URL + "/platform/api/org/users/" + userId + "/orgs";
        HttpMethod method = HttpMethod.GET;
        Map map = new HashMap();
        Map body = RestfulUtil.sendRestful(request, map, url, method);
        ArrayList data = (ArrayList) body.get("data");
        Map org = null;
        if (data != null) {
            org = (LinkedHashMap) data.get(0);
        }
        return org;
    }

    /**
     * 根据(userId)用户ID获取所属子系统
     */
    public Map getSudappsByUserId(HttpServletRequest request) {
        HttpSession session = request.getSession();
        String userId = (String) session.getAttribute("userId");
        String url = BASE_URL + "/platform/api/org/users/" + userId + "/subapps";
        HttpMethod method = HttpMethod.GET;
        Map map = new HashMap();
        Map body = RestfulUtil.sendRestful(request, map, url, method);
        return body;
    }

    /**
     * 根据组织id获取组织机构信息
     */
    public Map getOrgsByDeptId(HttpServletRequest request, String deptId) {
        String url = BASE_URL + "/platform/api/org/depts/" + deptId;
        HttpMethod method = HttpMethod.GET;
        Map map = new HashMap();
        Map body = RestfulUtil.sendRestful(request, map, url, method);
        Map data = (Map) body.get("data");
        /*Map org = null;
        if (data != null) {
            org = (LinkedHashMap) data.get(0);
        }*/
        return data;
    }


    /**
     * 根据userCode（即登录名）获取用户信息
     *
     * @param userCode
     * @return
     */
    public Map getUserByUserCode(HttpServletRequest request, String userCode) {
        String url = BASE_URL + "/platform/api/org/usercode/" + userCode;
        HttpMethod method = HttpMethod.GET;
        Map map = new HashMap();
        Map body = RestfulUtil.sendRestful(request, map, url, method);
        Map data = (Map) body.get("data");
        return data;
    }

}
